"""User C++ headers for writing TAPA programs."""

# Copyright (c) 2024 RapidStream Design Automation, Inc. and contributors.
# All rights reserved. The contributor(s) of this file has/have agreed to the
# RapidStream Contributor License Agreement.

load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test")
load("@rules_pkg//pkg:mappings.bzl", "pkg_filegroup", "pkg_files", "strip_prefix")
load("//bazel:header_extractor.bzl", "header_extractor")

bool_flag(
    name = "enable_coroutine",
    build_setting_default = True,  # Enable with `--//tapa-lib:tapa_enable_coroutine`
)

config_setting(
    name = "coroutine_enabled",
    flag_values = {":enable_coroutine": "True"},
)

cc_library(
    name = "tapa",
    srcs = glob(
        ["tapa/**/*.cpp"],
        exclude = ["tapa/**/*_test.cpp"],
    ),
    hdrs = glob([
        "tapa/**/*.h",
    ]) + ["tapa.h"],
    includes = ["."],
    local_defines = select({
        ":coroutine_enabled": ["TAPA_ENABLE_COROUTINE"],
        "//conditions:default": [],
    }),
    visibility = ["//visibility:public"],
    deps = [
        "//fpga-runtime:frt",
        "@boost//:coroutine2",
        "@boost//:thread",
        "@glog",
    ],
)

cc_test(
    name = "tapa-lib-test",
    size = "small",
    srcs = glob(["tapa/**/*_test.cpp"]),
    visibility = ["//visibility:public"],
    deps = [
        ":tapa",
        "@googletest//:gtest",
        "@googletest//:gtest_main",
    ],
)

filegroup(
    name = "include",
    srcs = glob([
        "tapa/**/*.h",
    ]) + ["tapa.h"],
    visibility = ["//visibility:public"],
)

header_extractor(
    name = "extra-runtime-include",
    visibility = ["//visibility:public"],
    deps = ["@glog"],
)

pkg_files(
    name = "pkg-lib",
    srcs = [":tapa"],
    prefix = "usr/lib",
    strip_prefix = strip_prefix.files_only(),
    visibility = ["//visibility:public"],
)

pkg_files(
    name = "pkg-include",
    srcs = [":include"],
    prefix = "usr/include",
    strip_prefix = strip_prefix.from_pkg(),
    visibility = ["//visibility:public"],
)

pkg_files(
    name = "pkg-extra-runtime-include",
    srcs = [":extra-runtime-include"],
    prefix = "usr/include",
    strip_prefix = strip_prefix.from_pkg("extra-runtime-include"),
    visibility = ["//visibility:public"],
)

pkg_files(
    name = "pkg-extra-runtime-deps",
    srcs = [
        "@boost//:context",  # for boost.coroutine2
        "@boost//:thread",
        "@glog",
    ],
    prefix = "usr/lib",
    strip_prefix = strip_prefix.files_only(),
    visibility = ["//visibility:public"],
)

pkg_filegroup(
    name = "pkg",
    srcs = [
        ":pkg-include",
        ":pkg-lib",
    ],
    visibility = ["//visibility:public"],
)

pkg_filegroup(
    name = "pkg-portable",
    srcs = [
        ":pkg",
        ":pkg-extra-runtime-deps",
        ":pkg-extra-runtime-include",
    ],
    visibility = ["//visibility:public"],
)
